package edu.uw.tcss360.group6.gui.MainCards;

import javax.swing.*;

import edu.uw.tcss360.group6.gui.GUI;
import edu.uw.tcss360.group6.gui.Actions.RegisterAction;
import edu.uw.tcss360.group6.model.Contest;
import edu.uw.tcss360.group6.model.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * edu.uw.tcss360.group6.model.User: Lemtzas
 * Author: David Sharer
 * Author: Mais Abdulwahab
 * Author: Landis Huey
 * Date: 2/20/13
 * Time: 9:46 PM
 * To change this template use File | Settings | File Templates.
 */
@SuppressWarnings("serial")
public class LoginPanel extends MainCard {
	JButton my_login = new JButton("LOGIN");
	JButton my_register = new JButton("REGISTER");
	JTextField my_userfield = new JTextField();
	JTextField my_passfield = new JTextField();
    public LoginPanel() {
        super();
        GridLayout bl = new GridLayout(3,2);
        this.setLayout(bl);
        addElement(new JLabel("Username :"));
        addElement(my_userfield);
//        addElement(new JLabel("Password :")); //We do not have passwords - David
//        addElement(my_passfield);
        addElement(my_login);
        //addElement(my_register); //we can skip this, as there's a button on the side to register - David
		this.setBorder(BorderFactory.createEmptyBorder(180, 50, 200, 50));
		my_login.addActionListener(new ActionListener()
	    {
	      public void actionPerformed(final ActionEvent an_event)
	      {
	    	//buildMainPage();
	    	//Stuff for model
	    	//A string that has the user name for the model
	    	String user_name = my_userfield.getText();
	    	if(user_name.isEmpty())
	    	{
	    		JOptionPane.showMessageDialog(null, "Enter username!");
	    	}
            else
            {
                User user = Contest.instance().getUser(user_name);
                if(user == null) {
                    JOptionPane.showMessageDialog(null, "Invalid username!");
                    GUI.instance().my_side_panel.logpanel.log("Username '" + user_name + "' was not found. Perhaps you should try registering?");

                } else {
                    //GUI.instance().my_side_panel.datacardpanel.my_login_card.add(new JButton(new EntryAction()));//This is not how you are supposed to do this -David
                    GUI.instance().currentUser = user;
                    String type = GUI.instance().currentUser.getClass().getSimpleName().toLowerCase();
                    GUI.instance().my_side_panel.logpanel.log("Welcome! Logged in as '" + user.getName() + "'. You are a '" + type + "'.");
                    GUI.instance().my_side_panel.datacardpanel.setCard(type); //switch to datacard panel based on class type
                    //GUI.instance().my_main_panel.setPreviousCard("login"); //they should not be able to go back from there, as it would log them out and cause state problems. - David
                    GUI.instance().my_main_panel.setCard(type + "-splash"); //switch to splash panel based on class type
                    GUI.instance().my_main_panel.clearCardHistory();
                }
            }
//	    	//String that has the password for the model //The model has no password, it was not required -  David
//	    	String password = my_passfield.getText();
//	    	if(password.isEmpty())
//	    	{
//	    		JOptionPane.showMessageDialog(null, "Enter A password!");
//	    	}

         //TODO: Password not used


        // my_active_user = my_contest.getUser(user_name);
         //if user_name is invalid, my_active_user will be null afterward
         //TODO: ERROR HANDLING FOR INVALID USERNAME
	    	
	    	//GUI.instance().my_side_panel.datacardpanel.setCards(new String[]{"edu.uw.tcss360.group6.model.Entry","back","Home"});
	      }
	    });
		my_register.addActionListener(new RegisterAction());

    }

    private void addElement(JComponent c) {
        c.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.add(c);
    }
}
